$(function() {
    //注册按钮点击事件
    $(".btn-primary").click(function() {
        //获取用户名和密码和确认密码
        var username = $("input[type='text']").val();
        var password = $("input[type='password']").val();
        var rePassword = $("input[type='password']:eq(1)").val();
        // 先行验证
        if (username === "") {
            alert("用户名不能为空");
            return;
        }
        if (password === "") {
            alert("密码不能为空");
            return;
        }
        if (rePassword === "") {
            alert("确认密码不能为空");
            return;
        }
        if (password !== rePassword) {
            alert("两次密码不一致");
            return;
        }
        var data = {
            username: username,
            password: password,
            rePassword: rePassword
        }
        //发送ajax请求
        $.ajax({
            type: "POST",
            url: "http://localhost:8888/user/reg",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function(data) {
                console.log(data);
                if(data.code === 20000) {
                    alert("注册成功");
                    location.href = "login.html";
                } else {
                    alert(data.message);
                }
            },
            error: function() {
                alert("网络故障，请检查");
            }
        });
    });
})
